package com.bygf.curve;

import com.bygf.curve.panel.ChartPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 * 标尺类
 *
 * @author sunyupei
 */
public class Mark {

    private ChartPanel chartPanel;
    //标尺颜色
    private Color color = Color.RED;
    //标尺画笔样式
    private BasicStroke stroke = new BasicStroke(1);
    //标尺像素坐标中心点
    private Point2D point;
    //标尺对应的X轴数值
    private double xValue;
    //标尺对应的Y轴数值
    private double yValue;
    //标尺对应的Y2轴数值
    private double y2Value;

    /**
     * 构造方法
     *
     * @param xValue
     * @param yValue
     * @param y2Value
     * @param chartPanel
     */
    public Mark(double xValue, double yValue, double y2Value, ChartPanel chartPanel) {
        this.xValue = xValue;
        this.yValue = yValue;
        this.y2Value = y2Value;
        this.chartPanel = chartPanel;
        point = new Point2D.Double(chartPanel.getXAxis().valueToPix(xValue), chartPanel.getYAxis().valueToPix(yValue));
    }

    /**
     * 标尺绘制方法
     *
     * @param g
     */
    public void draw(Graphics2D g) {
        g.setColor(color);
        g.setStroke(stroke);
        point = new Point2D.Double(chartPanel.getXAxis().valueToPix(xValue), chartPanel.getYAxis().valueToPix(yValue));
        Line2D line1 = new Line2D.Double(point.getX(), 0, point.getX(), chartPanel.getCurvePanel().getHeight());
        Line2D line2 = new Line2D.Double(0, point.getY(), chartPanel.getCurvePanel().getWidth(), point.getY());
        g.draw(line1);
        g.draw(line2);
        Rectangle2D rect = new Rectangle2D.Double(point.getX() - 2, point.getY() - 2, 4, 4);
        g.fill(rect);
    }

    /**
     * 判断指定点是否在标尺中心控制点上
     *
     * @param point
     * @return
     */
    public boolean isOnMark(Point point) {
        return this.point.distance(point) < 3;
    }

    /**
     * @return the color
     */
    public Color getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(Color color) {
        this.color = color;
    }

    /**
     * @return the stroke
     */
    public BasicStroke getStroke() {
        return stroke;
    }

    /**
     * @param stroke the stroke to set
     */
    public void setStroke(BasicStroke stroke) {
        this.stroke = stroke;
    }

    /**
     * @return the xValue
     */
    public double getxValue() {
        return xValue;
    }

    /**
     * @param xValue the xValue to set
     */
    public void setxValue(double xValue) {
        this.xValue = xValue;
    }

    /**
     * @return the yValue
     */
    public double getyValue() {
        return yValue;
    }

    /**
     * @param yValue the yValue to set
     */
    public void setyValue(double yValue) {
        this.yValue = yValue;
    }

    /**
     * @return the y2Value
     */
    public double getY2Value() {
        return y2Value;
    }

    /**
     * @param y2Value the y2Value to set
     */
    public void setY2Value(double y2Value) {
        this.y2Value = y2Value;
    }
}
